package org.pcorp.battlespace.construction.ui.admin.arm;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import org.pcorp.battlespace.construction.ui.admin.AbstractAdmin;
import org.pcorp.battlespace.construction.ui.admin.UiAdmin;

public class UiArmAdmin extends AbstractAdmin {
	private static final long serialVersionUID = 2676442515038942112L;

	private JTable tabArm; 
	private ModelTableArmAdmin model;
	
	public UiArmAdmin(UiAdmin uiadmin) {
		super(uiadmin);
		initTable();
		setLayout(new GridBagLayout());
		GridBagConstraints gbc = new GridBagConstraints();
		
		gbc.gridx = gbc.gridy = 0;
		gbc.weightx = gbc.weighty = 0;
		gbc.gridwidth = gbc.gridheight = 1;
	    gbc.fill = GridBagConstraints.BOTH;
	  
	    //JLabel label = new JLabel("test");
	    add(getSaveButton(), gbc);
	    
	    gbc.gridy = 1;
		gbc.weightx = gbc.weighty = 1;
	    // scroll
	    JScrollPane scrollPane = new JScrollPane(tabArm);
	    add(scrollPane, gbc);
	}
	
	private JButton getSaveButton() {
		JButton add = new JButton("save");
		add.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				uiAdmin.getServiceAdmin().saveAllArmement(model.getArmements());
			}
		});
		return add;
	}
	
	private void initTable() {
		//model = new ModelTableArmAdmin(Armements.getArmements("../construction-metier/arms.xml"));
		model = new ModelTableArmAdmin(uiAdmin.getServiceAdmin().getAllArmement());
		tabArm = new TableArmAdmin(model, uiAdmin); 
	}
}
